#ifndef __OP_DEFINES_H__
#define __OP_DEFINES_H__

#include "odbc_sys.h"

/*
@typedef env_t
@struct env_t
Driver internal structure for environment(HENV).
 */
typedef struct env_t
{
	int magic; /*	Magic cookie. */
	int ov3; /*  True for SQL_OV_ODBC3. */
#if defined(_WIN32) || defined(_WIN64)
	CRITICAL_SECTION cs; /* For serializing most APIs.	*/
	DWORD owner; /* Current owner of CS or 0 .	*/
#endif
	struct dbc_t *dbcs; /* Asociated dbcs.*/
} env_t;

/*
 @typedef dbc_t
 @struct dbc
 Driver internal structure for database connection (HDBC).
 */
typedef struct dbc_t 
{
	int magic; /* Magic cookie*/  
	env_t  *env; /* Pointer to environment*/  
	struct dbc_t *next; /* Pointer to next dbc_t*/ 
	struct stmt_t *cur_op3stmt; /* Current stmt_t executing openPlant statement*/
	char *db_name; /* openPlant database name*/ 
	char *dsn; /* ODBC data source name*/     
	int	timeout; /* Lock timeout value*/     
	long t0; /* Start time for openPlant busy handler*/  
	struct stmt_t *stmt; /* stmt_t list of this dbc_t*/   
	int	native_error; /* Native error code*/  
	char sql_state[6]; /* SQL state for SQLError()*/    
	SQLCHAR log_msg[1024]; /*	Message for SQLError()*/ 
	int	op3_stmt_row_num; /* Current row number*/   
} dbc_t;

/* 
 @typedef COL
 @struct COL
 Internal structure to describe a column in a result set.
 */ 
typedef struct col_t {
	char* db; /*Database name. */
	char* table; /*	Table name. */
	char* col_name; /*Column name. */
	char* alias;
	int	type; /*Data type of column.*/
	char*  data;
	int	len; /*data.length()*/
	int	nosign; /*Unsigned type. */
	int	not_null; /*NOT NULL constraint on column. */
} col_t; 

/* 
 @typedef BINDCOL
 @struct BINDCOL
 Internal structure for bound column (SQLBindCol).
 */
typedef struct bind_col_t
{
	SQLSMALLINT	type; /* ODBC type.*/
	SQLINTEGER max; /* Max size of value buffer	*/
	SQLLEN *lenp; /* Value return,actual size of value buffer	*/
	SQLPOINTER valp; /*	Value buffer.	*/
	int index; /* Index of column in result.*/
	int offs; /* Byte offset for SQLGetData().	*/
} bind_col_t; 

/*
 @typedef bind_param_t
 @struct bind_param_t
 Internal structure for bound parameter (SQLBindParameter).
 */
typedef struct bind_param_t
{
	int odbc_type; /* odbc type */
        int sql_type;  /* SQL types */

	/* From SQLBindParameter(). */
	int coldef;
       	int scale;

	SQLLEN max;   /* Max. size size of parameter buffer */
	SQLLEN *lenp; /* Actual size of parameter buffer */
	SQLLEN *lenp0; /* Actual size of parameter buffer. Initial value. */

	void *param; /*	Parameter buffer */
	void *param0; /* Parameter buffer. Initial value */

	int inc; /*	Increment for parameter set size > 1 */
	int bound; /* True when SQLBindParameter() called. */
	int offs, len; /* Offset/length for SQLParamData()/SQLPutData() */
	char strbuf[64]; /*	String buffer for scalar data	 */
} bind_param_t;

/*
 @typedef stmt_t
 @struct stmt
 Driver internal structure representing SQL statement (HSTMT).
 */
typedef struct stmt_t {
	struct stmt_t *next; /* Linkage for stmt_t list in dbc_t */
	HDBC dbc; /* Pointer to dbc_t */

	bind_col_t bkmrkcol; /* Bookmark bound column */
	bind_col_t *bindcols;

	col_t *cols; /* Result column array */
	col_t *dyncols; /* Column array, but malloc()ed */

	bind_param_t *bindparms; /* Array of bound parameters	 */
	SQLCHAR cursorname[32]; /* cursor name */
	char *query; /* raw SQL query string */
	int query_len; /* add for error HY090 */
	int isselect; /* > 0 if query is a SELECT statement */
	int ncols; /* Number of result columns */
	int nbindcols; /* Number of entries in bindcols */
	int nbindparms; /* Number bound parameters */	
	int nparams; /* Number of parameters in query */
	int nrows; /* Number of result rows	*/
	int rowp; /* Current result row.*/

	int	native_error; /* Native error code		*/
	char sql_state[6]; /* SQL state for SQError()	 */
	SQLCHAR	log_msg[1024]; /* Message for SQLError()	 */
	int	no_wchar[2]; /* Don't try to use WCHAR	 */
	int	long_names; /* Don't shorten column names  */
	int	retr_data; /* SQL_ATTR_RETRIEVE_DATA */
	SQLUINTEGER	rowset_size; /* Size of rowset */
	SQLUSMALLINT *row_status; /* Row status pointer */
	SQLUSMALLINT *row_status0; /* Internal status array */
	SQLUSMALLINT row_status1; /* Internal status array for 1 row rowsets	 */	
	SQLUINTEGER	*row_count; /* Row count pointer  */
	SQLUINTEGER	row_count0; /* Row count */
	SQLUINTEGER	paramset_size; /* SQL_ATTR_PARAMSET_SIZE */
	SQLUINTEGER	paramset_count;	/* Internal for paramset	 */
	SQLUINTEGER	paramset_nrows;	/* Row count for paramset handling	  */
	SQLUINTEGER	bind_type; /* SQL_ATTR_ROW_BIND_TYPE */
	SQLUINTEGER	*bind_offs; /* SQL_ATTR_ROW_BIND_OFFSET_PTR */
	int cur_type; /* Cursor type */
	char *bincell; /* Cache for blob data */
	char *bincache; /* Cache for blob data */
	int	binlen; /* Length of blob data	 */
	int	guessed_types; /* Flag for op_prepare()/op_execute()	 */	
} stmt_t;
#endif
